package cn.edu.nju.setting;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.json.JSONArray;
import org.json.JSONObject;

import cn.edu.nju.R;
import cn.edu.nju.storage.StoreDataProvider;

import android.app.Activity;

public class TagProvider {
	private static TagProvider provider;
	private List<String> tagCategories;
	private String serverUrl;
	private Activity activity;
	private Map<Integer, Long>tag_dir;
	
	public static TagProvider getInstance(Activity activity){
		if(provider == null){
			provider = new TagProvider(activity);
		}
		return provider;
	}
	
	private TagProvider(Activity activity){
		this.activity = activity;
		this.serverUrl = activity.getString(R.string.server_url) + "tag.php";
		tag_dir = new HashMap<Integer, Long>();
	}
	
	public List<String> getTagCategories(){
		tagCategories = new ArrayList<String>();
		
		try {
			HttpClient client = new DefaultHttpClient();
			HttpContext localContext = new BasicHttpContext();
			HttpPost post = new HttpPost(
					serverUrl);

			MultipartEntity entity = new MultipartEntity(
					HttpMultipartMode.BROWSER_COMPATIBLE);

			entity.addPart("id", new StringBody("" + StoreDataProvider.getInstance(activity).getUserId()));
			entity.addPart("option", new StringBody("getTagDir"));

			post.setEntity(entity);

			HttpResponse response = client.execute(post, localContext);
			HttpEntity httpEntity = response.getEntity();
			InputStream input = httpEntity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					input));

			String context = reader.readLine();
			reader.close();
			
			JSONArray array = new JSONArray(context);
			for (int i = 0; i < array.length(); i++) {
				JSONObject object = array.getJSONObject(i);
				tagCategories.add(object.getString("name"));
				tag_dir.put(i, object.getLong("id"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return tagCategories;
	}
	
	// get tags in category whose index is position
	public List<String> getTagsInCategory(int position){
		List<String> tags = new ArrayList<String>();
		try {
			HttpClient client = new DefaultHttpClient();
			HttpContext localContext = new BasicHttpContext();
			HttpPost post = new HttpPost(
					serverUrl);

			MultipartEntity entity = new MultipartEntity(
					HttpMultipartMode.BROWSER_COMPATIBLE);

			entity.addPart("id", new StringBody("" + tag_dir.get(position)));
			entity.addPart("option", new StringBody("getTags"));

			post.setEntity(entity);

			HttpResponse response = client.execute(post, localContext);
			HttpEntity httpEntity = response.getEntity();
			InputStream input = httpEntity.getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					input));

			String context = reader.readLine();
			reader.close();
			
			JSONArray array = new JSONArray(context);
			for (int i = 0; i < array.length(); i++) {
				tags.add(array.getJSONObject(i).getString("name"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return tags;
	}
	
}
